package org.example.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.example.admin.annotation.OperationLog;
import org.example.admin.mapper.OrdersMapper;
import org.example.admin.service.OrdersService;
import org.example.entity.Orders;
import org.example.entity.OrdersAndAddress;
import org.example.entity.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@Controller
public class OrdersController {

    private OrdersService ordersService;
    @Resource
    private OrdersMapper ordersMapper;
    public OrdersController(OrdersService ordersService) {
        this.ordersService = ordersService;
    }

    //查询全部订单并分页
    @ResponseBody
    @GetMapping("/order")
    public TableData<OrdersAndAddress> getPageOrders(Integer offset, Integer limit){
        List<OrdersAndAddress> pageOrders = ordersService.getPageOrders(offset, limit);
        System.out.println(pageOrders);
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        Integer integer = ordersMapper.selectCount(ordersQueryWrapper);
       TableData<OrdersAndAddress> ordersTableUser = new TableData<>();
       ordersTableUser.setRows(pageOrders);
            ordersTableUser.setTotal(integer);
            return ordersTableUser;
    }

    //修改订单地址
    @OperationLog(value = "修改订单地址",requireParamInfo = true)
    @ResponseBody
    @GetMapping("/address/{AddressOld}/{AddressNew}")
    public Object update(@PathVariable("AddressOld") String ono ,@PathVariable("AddressNew") String AddressNew) {
        Map<String, Integer> map = new HashMap<>();
        int update = 0;

        update = ordersService.update(ono, AddressNew);

        return update;
    }
}
